package com.zxgllhh.generator;

import java.io.File;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.commons.io.FileUtils;

import com.zxgllhh.domain.ConnectionContext;
import com.zxgllhh.domain.Table;
import com.zxgllhh.run.TableGeneratorThread;
import com.zxgllhh.utils.PropertyUtils;

/**
 * 
 * @author Zhao,XingGuo
 * @version 2010-01-21 生成器的大管家
 *
 */
public class GenaratorManager {

	public static void go(List<Table> tableList) throws Exception {
		long time = System.currentTimeMillis();
		//init
		String databaseName = ConnectionContext.getConnection().getMetaData().getDatabaseProductName();
		
		//File file = new File("E://generatorCode");
		//FileUtils.deleteDirectory(file);
		
		//FileUtils.forceMkdir(new File("E://generatorCode/src/com/"+companyName+"/" + projectName + "/web/struts/manager/admin"));
		//FileUtils.forceMkdir(new File("E://generatorCode/src/com/"+companyName+"/" + projectName + "/domain"));
		//FileUtils.forceMkdir(new File("E://generatorCode/src/com/"+companyName+"/" + projectName + "/service/impl"));
		//FileUtils.forceMkdir(new File("E://generatorCode/src/com/"+companyName+"/" + projectName + "/dao/ibatis/maps"));
		
		ExecutorService exec = Executors.newFixedThreadPool(10);
		int j = 0;
		int avg = tableList.size()/20+1;
		for(int i = 0 ; i < avg ; i++ ){
			List<Table> subTableList = null;
			if(tableList.size() < j+20){
				subTableList = tableList.subList(j, tableList.size());
			}else{
				subTableList = tableList.subList(j, j+20);
			}
			j = j+20;
			
			TableGeneratorThread run = new TableGeneratorThread();
			run.setDatabaseName(databaseName);
			run.setTableList(subTableList);
			exec.execute(run);
		}
		exec.shutdown();

		while (!exec.isTerminated()){
		}
		
		System.out.println("one file : " + (System.currentTimeMillis() - time) + " milliseconds");
		
		FacadeGenarator.go(tableList);
		FacadeImplGenarator.go(tableList);
		SqlMapConfigGenarator.go(tableList);
		System.out.println("two file : " + (System.currentTimeMillis() - time) + " milliseconds");
	}

}
